package net.a6polo.a6poloweibo;

import java.io.IOException;

import net.a6polo.a6poloweibo.sina.domain.Uid;
import net.a6polo.a6poloweibo.sina.domain.User;
import net.a6polo.renren.RenrenApi;
import net.a6polo.renren.ui.RenrenFeedActivity;
import net.a6polo.service.Service;
import net.a6polo.service.SnsType;
import net.a6polo.service.domain.Account;

import com.google.gson.Gson;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.AccountAPI;
import com.weibo.sdk.android.api.UsersAPI;
import com.weibo.sdk.android.net.RequestListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class IndexActivity extends Activity implements OnClickListener{

	private Weibo mWeibo;
	
	private RenrenApi renrenApi;
	
	private Oauth2AccessToken accessToken;
	
	private Service service;
	
	private Account sinaAccount;
	
	private Account renrenAccount;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_index);
		
		service = new Service(IndexActivity.this);
		service.getReadableDatabase();
		
		WebView.enablePlatformNotifications();
		
		
		LinearLayout sinaLogin = (LinearLayout)findViewById(R.id.sina_logo);
		LinearLayout renrenLogin = (LinearLayout)findViewById(R.id.renren_logo);
		LinearLayout qzoneLogin = (LinearLayout)findViewById(R.id.qzone_logo);
		
		sinaLogin.setOnClickListener(this);
		renrenLogin.setOnClickListener(this);
		qzoneLogin.setOnClickListener(this);

	}

	class AuthDialogListener implements WeiboAuthListener {

		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");			
			sinaAccount = new Account();
			sinaAccount.setAccessToken(token);
			sinaAccount.setExpiresIn(expires_in);
			
			accessToken = new Oauth2AccessToken(token, expires_in);
			
			AccountAPI accountApi = new AccountAPI(accessToken);
			accountApi.getUid(new AccountRequestListener());

		}

		@Override
		public void onCancel() {
			Toast.makeText(getApplicationContext(), "Auth cancel",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onWeiboException(WeiboException e) {
			Toast.makeText(getApplicationContext(),
					"Auth exception : " + e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}

		@Override
		public void onError(WeiboDialogError e) {
			Toast.makeText(getApplicationContext(),
					"Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}
		
	
	class AccountRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			Gson gson = new Gson();
			Uid uid = gson.fromJson(json, Uid.class);
			sinaAccount.setUid(uid.getUid());
			if(null != uid && uid.getUid() > 0) {
				UsersAPI usersApi = new UsersAPI(accessToken);
				usersApi.show(uid.getUid() , new UsersRequestListener());
			}
			
		}

		@Override
		public void onError(WeiboException e) {
			Toast.makeText(getApplicationContext(),
					" error : " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onIOException(IOException e) {
			Toast.makeText(getApplicationContext(),
					"exception : " + e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}
		
	}
	
	class UsersRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			Gson gson = new Gson();
			User user = gson.fromJson(json, User.class);
			
			sinaAccount.setUserName(user.getName());
			sinaAccount.setType(SnsType.SINA_WEIBO);
			long id = service.addAccount(sinaAccount);
			Log.d("a6polo", "---------sina account id:" + id); 
			Intent intent = new Intent();
			intent.setClass(IndexActivity.this, IndexActivity.class);
			startActivity(intent);
			
			finish();
		}

		@Override
		public void onError(WeiboException e) {
			Toast.makeText(getApplicationContext(),
					" error : " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onIOException(IOException e) {
			Toast.makeText(getApplicationContext(),
					"exception : " + e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.sina_logo: {
			sinaButtonClick();
			break;
		}
		case R.id.renren_logo: {
			renrenButtonClick();
			break;
		}
		case R.id.qzone_logo: {

			break;
		}
		default:
			break;
		}
		
	}
	
	private void sinaButtonClick() {
		sinaAccount = service.getLastAccountByType(SnsType.SINA_WEIBO);
		Log.d("a6polo", "新浪账号:" + sinaAccount);
		
		if(null == sinaAccount) {
			mWeibo = Weibo.getInstance("3113749730", "http://a6polo.cn");
			mWeibo.authorize(IndexActivity.this, new AuthDialogListener());
		} else {
			Intent intent = new Intent();
			intent.setClass(IndexActivity.this, MainTabActivity.class);
			startActivity(intent);
		}
		
	}
	
	private void renrenButtonClick() {		
		renrenAccount = service.getLastAccountByType(SnsType.RENREN);
		Log.d("a6polo", "人人账号:" + renrenAccount);
		
		if(null == renrenAccount) {
			Intent intent = new Intent();
			intent.setClass(IndexActivity.this, RenrenIndexActivity.class);
			startActivity(intent);
			finish();
			
		} else {
			Intent intent = new Intent();
			intent.setClass(IndexActivity.this, RenrenFeedActivity.class);
			startActivity(intent);
		}

	}
	
}
